vimfandomcom-20200223-history
Linting
Vim has the ability to work with various linting systems. This page covers some strategies for working with linters. The naive approach To understand why the methods described below are useful, we first describe the "naive" approach to linting. In this approach, the user is initially editing a file in Vim. To run the linter, the user might do one of the following: * Open a new terminal window and run the linting command there. * Hit CTRL-Z to send Vim to the background, run the linting command, then use fg to bring Vim back to the foreground. * Use :! in Vim to run the linting command. (See ) * Open a new split window in tmux or similar programs and run the linting command there. All the of the above approaches require (1) manually specifying the linting program and (2) manually reading the error messages to navigate to some place in the code (i.e. the place where the errors occur). It would be nice if these steps can be automated, so that the user need only think "lint this!" and be given a list of places to jump to. Compiler plugins In stock Vim, the way to work with linters is to load warnings and other messages from the linters into the quickfix list and jump to them using quickfix commands. See for detailed documentation. To let Vim know which linter to use, the 'makeprg' and 'errorformat' options can be set. Compiler plugins are special plugins in Vim that set these options to predefined values so that the user need only type :compiler {name} to use the intended compiler. After specifying the compiler once, the user can from that point just invoke :make. Note that these are called "compiler" plugins because initially they were used exclusively with compilers and build systems such as make, gcc, and javac, but they can be used for any program that takes as inputs certain files (e.g. the current file) and outputs locations (e.g. the line and column of errors). In particular, compiler plugins can be used for linters. Vim comes with numerous compiler plugins, which are stored in $VIMRUNTIME/compiler/. More information about compiler plugins can be found at . To use Pylint as an example: " Set the compiler to pylint. This is local to the buffer. :compiler pylint " Set the compiler to pylint. This is global. :compiler! pylint " Set the compiler for Python files to pylint autocmd FileType python compiler pylint " Check the current file :make % " Check all Python files in the current directory :make *.py Dispatch.vim Tim Pope's dispatch.vim plugin eases the switching of compilers and also allows asynchronous linting. :Dispatch pylint % Syntastic Syntastic is a popular syntax checking plugin. Neomake Neomake is a recent plugin that uses the job control features of Vim 8 and Neovim to asynchronously run programs. Asynchronous Lint Engine (ALE) ALE is a more recent plugin that uses the asynchronous jobs features of Vim 8 and Neovim to asynchronously run linting programs on the buffer. This means saving the buffer to the filesystem is not necessary. It comes bundled with support for numerous linting programs, so often the linting programs simply need to be installed somewhere sensible to start checking your code. See also * * Errorformats * Errorformat and makeprg * Autoselect the right compiler using the filetype External links * "lint" tag on Vim Awesome * Search for "lint" on Vim Awesome * Scripts containing "lint" in Vim Scripts submitted to Vim.org